package com.pz.gulimall.search.controller;

import com.pz.gulimall.common.exception.BizCodeEnum;
import com.pz.gulimall.common.to.es.SkuEsModelTo;
import com.pz.gulimall.common.utils.R;
import com.pz.gulimall.search.service.ProductService;
import com.pz.gulimall.search.vo.req.SearchParamReq;
import com.pz.gulimall.search.vo.res.SearchResVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("es/product")
public class EsProductController {
    @Resource
    private ProductService productService;

    /**
     * 创建检索、保存商品
     * @param skuEsModelTos 商品数据模型
     * @return
     */
    @PostMapping("/sku/save")
    public R saveSku(@RequestBody @Valid List<SkuEsModelTo> skuEsModelTos){
        boolean flag = productService.productStatusUp(skuEsModelTos);
        if(flag){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }

    @RequestMapping("/sku/search")
    public R search(@RequestBody @Valid SearchParamReq searchParamReq){
        SearchResVo res = productService.search(searchParamReq);
        return R.ok().put("data", res);
    }
}
